/****************************************************
    ScriptName:        Boss4.cs
    Author:            摸鱼程序男
    CreatTime:    2020/12/26 20:28:18
    Description:     Nothing
*****************************************************/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using DG.Tweening;
using System.Linq;
using BTAI;
using ZYKTool;
using ZYKTool.Timer;
using LongZhiJie;


namespace HQL
{
    public class Boss4 : BossBase

    {
        public GameObject bullet_SP;
        public Transform[] Poss;
        private int _damage;
        private float _speed;
        public override void Init()
        {

            base.Init();
            _aiRoot.OpenBranch(

                BT.If(CanAttack).OpenBranch(
                    BT.Call(Attack),
                    BT.Wait(1f)
                )
            //BT.If(CanWalk).OpenBranch(
            //BT.Call(Walk)
            //)
            /*
           BT.While(die1).OpenBranch(
                 BT.Wait(1),
                 BT.Call(换阶段)
                  )
            */
            ); 
            _AttackTimer = 1;
            _SR = transform.GetOrAddComponent<SpriteRenderer>();
            _MyColor = _SR.color;
            _damage = 1;
            _speed = 30;
            _Blood = TapTapStaticValue.BossAttributes[BossName.Boss4].Blood;
            BossName = BossName.Boss4;
        }
        public override void BossHunt(float damage)
        {
            TapTapStaticValue.TapTapModelManger.Damage(BossName.Boss4, damage);
            _Blood = TapTapStaticValue.BossAttributes[BossName.Boss4].Blood;
            if (TapTapStaticValue.BossAttributes[BossName.Boss4].Blood <= 0)
            {
                Die(BossName.Boss4);
                TapTapStaticValue.TapTap升级系统.TapTapStartUpdate();
            }
            else if (TapTapStaticValue.BossAttributes[BossName.Boss4].Blood <= 1000 && 当前阶段 < 2) 
            {
                换阶段();
            }
     
        }

        protected override void Attack()
        {
            if (_已经死亡) return;
            //Debug.LogError("attack");
            switch (当前阶段)
            {
                case 1:
                    Attack1();
                    break;
                case 2:
                    Attack2();
                    //TODO:贺启亮 换阶段之后如果需要改变参数写这里
                    break;
            }
        }
        public override void 换阶段()
        {
            base.换阶段();
            //Debug.LogError("二级模式切换阶段！");
            _AttackTimer = 1f;
            _damage = 2;
            _speed = 35;
            //todo:贺启亮根据血量 让他换阶段就行
        }
        protected void Attack1()
        {
            if (UnityEngine.Random.Range(0, 10)<5)
            {
                _SR.color = Color.blue;
                ZYKTool.Timer.ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask(
                     () =>
                     {
                         if (_已经死亡) return;
                         int dashNUM = UnityEngine.Random.Range(0, 4);
                         dash(Poss[dashNUM]);
                     }, 2f, 1);
            }
            else
            {
        
                ZYKTool.Timer.ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask(
                       () =>
                       {
                           if (_已经死亡) return;


                           Shoot(_damage,_speed);
                       }, 2f, 1);



            }
        }
        protected void Attack2()
        {
            Attack1();
            //todo：贺启亮你这个attack2呢
        }
        protected void dash(Transform target)
        {
            if (_已经死亡) return;
            PlaySound();
            //transform.position = Vector3.MoveTowards( transform.position, new Vector3(p, x, 0), 2.4f);
            if (transform.position.x - TapTapStaticValue.Player.transform.position.x < 8 && UnityEngine.Random.Range(0, 10) < 5)
            {
                _SR.color = Color.blue;
                transform.DOKill();               
                transform.DOMove(TapTapStaticValue.Player.transform.position, 0.8f);              
                ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                     (() =>
                     {
                         if (_已经死亡) return;
                         transform.DOKill();
                         _SR.color = _MyColor;
                         _AttackOver = true;


                     }, 0.8f, 1);//停止撞击



            }
            else
            {
                _SR.color = Color.red;
                transform.DOKill();
                // Debug.LogError("dash");
                transform.DOMove(TapTapStaticValue.Player.transform.position, 3);
                //var dashStop = UnityEngine.Random.Range(0, 1);
                ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                     (() =>
                     {
                         if (_已经死亡) return;
                         transform.DOKill();
                         _SR.color = _MyColor;
                         _AttackOver = true;
                     }, 3, 1);//停止撞击
            }
            
        }
        protected void Shoot(int damage, float speed)
        {
            if (_已经死亡) return;
            PlaySound();
            _SR.color = Color.yellow;
            int j = 0;         
            for (int i = 0; i < 6; i++, j++)//散弹枪
            {               
                Vector3 dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
                GameObject bullet = Instantiate(bullet_SP, transform.position, Quaternion.Euler(dir));
                BossBullet bossbullet = bullet.GetOrAddComponent<BossBullet>();
                bossbullet.Damage = damage;
                bossbullet.speed = speed;
                bossbullet.dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
                bullet.transform.Rotate(new Vector3(0, 0, j * 20));
                _AttackOver = true;
                _SR.color = _MyColor;
                if ((TapTapStaticValue.Player.transform.position.x - transform.position.x) <= 10&&UnityEngine.Random.Range(0,10)<3) 
                {
                    CZ(_damage,_speed);
                }
            }
        }
        protected void CZ(int damage, float speed) 
        {


            if (_已经死亡) return;
            PlaySound();
            _SR.color = Color.yellow;
            int j = 0;
            for (int i = 0; i <6 ; i++, j++)//散弹枪
            {
                Vector3 dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
                GameObject bullet = Instantiate(bullet_SP, transform.position, Quaternion.Euler(dir));
                BossBullet bossbullet = bullet.GetOrAddComponent<BossBullet>();
                bossbullet.Damage = damage;
                bossbullet.speed = speed;
                bossbullet.dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
                if (UnityEngine.Random.Range(0, 10) < 5)
                {
                    bullet.transform.Rotate(new Vector3(0, 0, j * 5));
                }
                else
                {

                    bullet.transform.Rotate(new Vector3(0, 0, j * 10));
                }
                _AttackOver = true;
                _SR.color = _MyColor;
            }

        }
    }
}